package com.liuyucheng.springbean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class Person implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {

    @Value("liuyucheng")
    private String name;

    @Value("北京")
    private String addr;

    @Value("139")
    private int phone;

    private BeanFactory beanFactory;

    private String beanName;

    public Person() {
        System.out.println("调用构造方法");
    }

    //初始化方法的注解方式  等同与init-method
    @PostConstruct
    public void initMethod(){
        System.out.println("调用initMethod....");
    }

    //销毁方法的注解方式  等同于destroy-method
    @PreDestroy
    public void destroyMethod(){
        System.out.println("调用destroyMethod....");
    }

    //BeanFactoryAware
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

    }

    //BeanNameAware
    @Override
    public void setBeanName(String s) {

    }

    //InitializingBean
    @Override
    public void afterPropertiesSet() throws Exception {

    }

    //DisposableBean
    @Override
    public void destroy() throws Exception {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("注入属性name");
        this.name = name;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        System.out.println("注入属性addr");
        this.addr = addr;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        System.out.println("注入属性phone");
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", addr='" + addr + '\'' +
                ", phone=" + phone +
                '}';
    }
}
